package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.BookAccuracySet;
import com.xmy.cultivate.entity.BookOrder;
import com.xmy.cultivate.entity.BookStudent;
import com.xmy.cultivate.entity.request.BookClassTypeCountRs;
import com.xmy.cultivate.entity.request.BookSchoolCountRs;
import com.xmy.cultivate.entity.request.BookSchoolRs;
import com.xmy.cultivate.mapper.BookOrderMapper;
import com.xmy.cultivate.mapper.BookStudentMapper;
import com.xmy.cultivate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 教材订购管理 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2025-01-04
 */
@Service
public class BookOrderServiceImpl extends ServiceImpl<BookOrderMapper, BookOrder> implements IBookOrderService {

    @Autowired
    BookOrderMapper bookOrderMapper;

    @Autowired
    IDictService iDictService;

    @Autowired
    ICourseTypeService iCourseTypeService;

    @Autowired
    BookStudentMapper bookStudentMapper;

    @Autowired
    @Lazy
    IBookOrderService iBookOrderService;

    @Autowired
    @Lazy
    IBookStudentService iBookStudentService;

    @Autowired
    IBookAccuracySetService iBookAccuracySetService;

    @Override
    public IPage<BookSchoolRs> getOrderSchool(IPage<?> page,
                                              Integer yearPart,
                                              Integer quarterNum,
                                              String subjectsId,
                                              String classTypeId,
                                              Integer lessonType,
                                              String schoolIdStr,
                                              Long bookTextId,
                                              Integer bookTextType,
                                              Integer isDetails,
                                              Long teacherId,
                                              Long schoolId,
                                              Integer yearClassId,
                                              String teacherName,
                                              String studentName,
                                              Integer lessonNum,
                                              Long gradeId,
                                              Integer subscribeType
    ) {
        Integer beforeLessonNum = 0;
        if (lessonNum != null) {
            if (lessonNum == 2) {
                beforeLessonNum = 1;
            } else if (lessonNum > 2) {
                beforeLessonNum = lessonNum - 2;
            }
        }

        Integer rade = 0;
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("class_type_id", classTypeId);
        wrapper.eq("subjects_id", subjectsId);
        BookAccuracySet bookAccuracySet = iBookAccuracySetService.getOne(wrapper);
        if (bookAccuracySet != null) {
            rade = bookAccuracySet.getRade();
        }

        if (bookTextId.equals(1900085161890938885L) && subjectsId.equals("1")) {
            return null;
        }

        IPage<BookSchoolRs> bookSchoolRsIPage = bookOrderMapper.getOrderSchool(page, yearPart, quarterNum, subjectsId, classTypeId, lessonType, schoolIdStr, bookTextId, bookTextType, isDetails, teacherId, schoolId, yearClassId, teacherName, studentName, gradeId, lessonNum, beforeLessonNum, rade, subscribeType);
        if (isDetails != 2) {
            for (BookSchoolRs bookSchoolRs : bookSchoolRsIPage.getRecords()) {
                bookSchoolRs.setBookCountList(bookOrderMapper.getSubCountList(yearPart, quarterNum, bookSchoolRs.getSchoolId(), subjectsId, classTypeId, lessonType, bookTextId, bookTextType, isDetails, bookSchoolRs.getTeacherId(), yearClassId, lessonNum, beforeLessonNum, rade, subscribeType));
            }
        }
        return bookSchoolRsIPage;
    }

    @Override
    public List<BookClassTypeCountRs> getOrderSchoolCountForClassType(Integer yearPart,
                                                                      Integer quarterNum,
                                                                      String subjectsId,
                                                                      String classTypeId,
                                                                      Integer lessonType,
                                                                      Long bookTextId,
                                                                      Integer bookTextType,
                                                                      Integer lessonNum,
                                                                      Integer isDetails,
                                                                      Integer subscribeType
    ) {


        Integer beforeLessonNum = 0;
        if (lessonNum != null) {
            if (lessonNum == 2) {
                beforeLessonNum = 1;
            } else if (lessonNum > 2) {
                beforeLessonNum = lessonNum - 2;
            }
        }

        Integer rade = 0;
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("class_type_id", classTypeId);
        wrapper.eq("subjects_id", subjectsId);
        BookAccuracySet bookAccuracySet = iBookAccuracySetService.getOne(wrapper);
        if (bookAccuracySet != null) {
            rade = bookAccuracySet.getRade();
        }

        return bookOrderMapper.getOrderSchoolCountForClassType(yearPart, quarterNum, subjectsId, classTypeId, lessonType, bookTextId, bookTextType, lessonNum, beforeLessonNum, rade, isDetails, subscribeType);
    }


    @Override
    public IPage<BookSchoolRs> getYetOrderSchool(
            IPage<?> page,
            Long orderId,
            Integer yearPart,
            Integer quarterNum,
            String subjectsId,
            String classTypeId,
            Integer lessonType,
            Integer type,
            Integer status,
            Integer orderType,
            Integer orderSubType,
            String schoolIdStr,
            Long bookTextId,
            Integer bookTextType,
            Integer isDetails,
            Long teacherId,
            Long schoolId,
            Integer yearClassId,
            String teacherName,
            String studentName,
            Long gradeId,
            Integer subscribeType,
            Integer lessonNum
    ) {

        Integer beforeLessonNum = 0;
        if (lessonNum != null) {
            if (lessonNum == 2) {
                beforeLessonNum = 1;
            } else if (lessonNum > 2) {
                beforeLessonNum = lessonNum - 2;
            }
        }

        Integer rade = 0;
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("class_type_id", classTypeId);
        wrapper.eq("subjects_id", subjectsId);
        BookAccuracySet bookAccuracySet = iBookAccuracySetService.getOne(wrapper);
        if (bookAccuracySet != null) {
            rade = bookAccuracySet.getRade();
        }

        IPage<BookSchoolRs> bookSchoolRsIPage = bookOrderMapper.getYetOrderSchool(page, orderId, yearPart, quarterNum, subjectsId, classTypeId, lessonType, type, status, orderType, orderSubType, schoolIdStr, bookTextId, bookTextType, isDetails, teacherId, schoolId, yearClassId, teacherName, studentName, gradeId, subscribeType, lessonNum, beforeLessonNum, rade);

        if (isDetails != 2) {
            for (BookSchoolRs bookSchoolRs : bookSchoolRsIPage.getRecords()) {
                if (isDetails == 1) {
                    bookSchoolRs.setBookCountList(bookOrderMapper.getYetSubCountList(yearPart, quarterNum, schoolId, subjectsId, classTypeId, lessonType, orderId, status, type, orderType, orderSubType, isDetails, bookTextId, bookSchoolRs.getTeacherId(), yearClassId, bookTextType, subscribeType, lessonNum, beforeLessonNum, rade));
                } else {
                    if (type == 2) {
                        bookSchoolRs.setBookCountList(bookOrderMapper.getYetSubCountList(yearPart, quarterNum, bookSchoolRs.getSchoolId(), subjectsId, classTypeId, lessonType, orderId, status, type, orderType, orderSubType, isDetails, bookTextId, bookSchoolRs.getTeacherId(), yearClassId, bookTextType, subscribeType, lessonNum, beforeLessonNum, rade));
                    } else {
                        bookSchoolRs.setBookCountList(bookOrderMapper.getYetSubCountList(yearPart, quarterNum, null, subjectsId, classTypeId, lessonType, bookSchoolRs.getOrderId(), status, type, orderType, orderSubType, isDetails, bookTextId, bookSchoolRs.getTeacherId(), yearClassId, bookTextType, subscribeType, lessonNum, beforeLessonNum, rade));

                    }
                }

            }
        }

        return bookSchoolRsIPage;
    }

    //获取教材发放
    @Override
    public List<BookSchoolRs> getDistributeOrderSchool(
            Long orderId,
            Integer yearPart,
            Integer quarterNum,
            String subjectsId,
            String classTypeId,
            Integer lessonType,
            Integer type,
            Integer status,
            Integer orderType,
            Integer orderSubType,
            Integer orderStatus,
            String schoolIdStr
    ) {
        return bookOrderMapper.getDistributeOrderSchool(orderId, yearPart, quarterNum, subjectsId, classTypeId, lessonType, type, status, orderType, orderSubType, orderStatus, schoolIdStr);
    }

    @Override
    public IPage<BookSchoolRs> getDistributeOrderSchoolDetails(
            IPage<?> page,
            Long orderId,
            Integer yearPart,
            Integer quarterNum,
            String subjectsId,
            String classTypeId,
            String yearClassId,
            Integer lessonType,
            Integer type,
            Integer orderType,
            Integer orderSubType,
            Integer orderStatus,
            Integer disType,
            String schoolId,
            String teacherId,
            String idList,
            String teacherName,
            String studentName,
            Long gradeId,
            String schoolIdStr,
            Integer isDetails,
            Long bookTextId,
            Integer bookTextType,
            Integer subscribeType,
            Integer lessonNum
    ) {

        Integer beforeLessonNum = 0;
        if (lessonNum != null) {
            if (lessonNum == 2) {
                beforeLessonNum = 1;
            } else if (lessonNum > 2) {
                beforeLessonNum = lessonNum - 2;
            }
        }

        Integer rade = 0;
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("class_type_id", classTypeId);
        wrapper.eq("subjects_id", subjectsId);
        BookAccuracySet bookAccuracySet = iBookAccuracySetService.getOne(wrapper);
        if (bookAccuracySet != null) {
            rade = bookAccuracySet.getRade();
        }

        return bookOrderMapper.getDistributeOrderSchoolDetails(page, orderId, yearPart, quarterNum, subjectsId, classTypeId, yearClassId, lessonType, type, orderType, orderSubType, orderStatus, disType, schoolId, teacherId, idList, teacherName, studentName, gradeId, schoolIdStr, isDetails, bookTextId, bookTextType, subscribeType, lessonNum, beforeLessonNum, rade);
    }

    @Override
    public List<BookSchoolCountRs> getGrantCountList(Integer yearPart, Integer quarterNum, Long schoolId, String subjectsId, String classTypeId, String yearClassId, Integer lessonType, Long orderId, Integer type, Integer orderType, Integer orderSubType, Long teacherId, String teacherName, Integer disType, Integer isDetails, Long bookTextId, Integer bookTextType, Integer subscribeType, Integer lessonNum) {

        Integer beforeLessonNum = 0;
        if (lessonNum != null) {
            if (lessonNum == 2) {
                beforeLessonNum = 1;
            } else if (lessonNum > 2) {
                beforeLessonNum = lessonNum - 2;
            }
        }

        Integer rade = 0;
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("class_type_id", classTypeId);
        wrapper.eq("subjects_id", subjectsId);
        BookAccuracySet bookAccuracySet = iBookAccuracySetService.getOne(wrapper);
        if (bookAccuracySet != null) {
            rade = bookAccuracySet.getRade();
        }

        return bookOrderMapper.getGrantCountList(yearPart, quarterNum, schoolId, subjectsId, classTypeId, yearClassId, lessonType, orderId, type, orderType, orderSubType, teacherId, teacherName, disType, isDetails, bookTextId, bookTextType, subscribeType, lessonNum, beforeLessonNum, rade);
    }

    @Override
    public IPage<BookSchoolRs> getRepertoryUnissuedList(IPage<?> page, Integer yearPart, Integer quarterNum, String subjectsId, String classTypeId, String yearClassId, Integer lessonType, String schoolId, String teacherName, String studentName, Long gradeId, Integer isDetails, Integer type, String idList, String schoolIdStr, Long bookTextId, Integer bookTextType, Long teacherId, Integer subscribeType, Integer lessonNum) {
        Integer beforeLessonNum = 0;
        if (lessonNum != null) {
            if (lessonNum == 2) {
                beforeLessonNum = 1;
            } else if (lessonNum > 2) {
                beforeLessonNum = lessonNum - 2;
            }
        }

        Integer rade = 0;
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("class_type_id", classTypeId);
        wrapper.eq("subjects_id", subjectsId);
        BookAccuracySet bookAccuracySet = iBookAccuracySetService.getOne(wrapper);
        if (bookAccuracySet != null) {
            rade = bookAccuracySet.getRade();
        }

        if (isDetails == 2) {
            // 详情
            return bookOrderMapper.getCatIssuedList(page, yearPart, quarterNum, subjectsId, classTypeId, yearClassId, lessonType, schoolId, teacherName, studentName, gradeId, isDetails, type, idList, schoolIdStr, bookTextId, bookTextType, subscribeType, lessonNum, beforeLessonNum, rade);
        } else {
            return bookOrderMapper.getRepertoryUnissuedList(page, yearPart, quarterNum, subjectsId, classTypeId, yearClassId, lessonType, schoolId, teacherName, studentName, gradeId, isDetails, type, idList, schoolIdStr, bookTextId, bookTextType, teacherId, subscribeType, lessonNum, beforeLessonNum, rade);
        }
    }

    @Override
    public List<BookSchoolCountRs> getRepertoryUnissuedCountList(Integer yearPart, Integer quarterNum, String subjectsId, String classTypeId, String yearClassId, Integer lessonType, Long schoolId, String teacherName, String studentName, Long gradeId, Integer isDetails, Integer type, Long bookTextId, Integer bookTextType, Long teacherId, Integer subscribeType, Integer lessonNum) {
        Integer beforeLessonNum = 0;
        if (lessonNum != null) {
            if (lessonNum == 2) {
                beforeLessonNum = 1;
            } else if (lessonNum > 2) {
                beforeLessonNum = lessonNum - 2;
            }
        }

        Integer rade = 0;
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("class_type_id", classTypeId);
        wrapper.eq("subjects_id", subjectsId);
        BookAccuracySet bookAccuracySet = iBookAccuracySetService.getOne(wrapper);
        if (bookAccuracySet != null) {
            rade = bookAccuracySet.getRade();
        }

        return bookOrderMapper.getRepertoryUnissuedCountList(yearPart, quarterNum, subjectsId, classTypeId, yearClassId, lessonType, schoolId, teacherName, studentName, gradeId, isDetails, type, bookTextId, bookTextType, teacherId, subscribeType, lessonNum, beforeLessonNum, rade);
    }

    @Override
    public BookStudent getNotIssued(Integer yearPart, Integer quarterNum, String subjectsId, String classTypeId, String yearClassId, Integer lessonType, Long schoolId, Long bookTextId) {
        return bookOrderMapper.getNotIssued(yearPart, quarterNum, subjectsId, classTypeId, yearClassId, lessonType, schoolId, bookTextId);
    }

    //下单详情
    @Override
    public IPage<BookSchoolRs> getYetOrderSchoolDetail(
            IPage<?> page,
            Long orderId,
            Integer yearPart,
            Integer quarterNum,
            String subjectsId,
            String classTypeId,
            Integer lessonType,
            Integer type,
            Integer status,
            Integer orderType,
            Integer orderSubType,
            String schoolIdStr,
            Integer bookTextType,
            Integer lessonNum,
            Integer subscribeType
    ) {

        Integer beforeLessonNum = 0;
        if (lessonNum != null) {
            if (lessonNum == 2) {
                beforeLessonNum = 1;
            } else if (lessonNum > 2) {
                beforeLessonNum = lessonNum - 2;
            }
        }

        Integer rade = 0;
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("class_type_id", classTypeId);
        wrapper.eq("subjects_id", subjectsId);
        BookAccuracySet bookAccuracySet = iBookAccuracySetService.getOne(wrapper);
        if (bookAccuracySet != null) {
            rade = bookAccuracySet.getRade();
        }

        return bookOrderMapper.getYetOrderSchoolDetail(page, orderId, yearPart, quarterNum, subjectsId, classTypeId, lessonType, type, status, orderType, orderSubType, schoolIdStr, subscribeType, lessonNum, beforeLessonNum, rade);
    }

    @Override
    public List<BookClassTypeCountRs> getBookCountForClassType(
            Integer yearPart,
            Integer quarterNum,
            String subjectsId,
            Integer lessonType,
            Integer status,
            Integer orderType,
            Integer orderSubType,
            Long bookTextId,
            Integer bookTextType,
            Integer subscribeType,
            Integer lessonNum
    ) {

        Integer beforeLessonNum = 0;
        if (lessonNum != null) {
            if (lessonNum == 2) {
                beforeLessonNum = 1;
            } else if (lessonNum > 2) {
                beforeLessonNum = lessonNum - 2;
            }
        }

        return bookOrderMapper.getBookCountForClassType(yearPart, quarterNum, subjectsId, lessonType, status, orderType, orderSubType, bookTextId, bookTextType, subscribeType, lessonNum, beforeLessonNum);
    }


    @Override
    @Transactional
    public Integer addBookOrder(BookOrder bookOrder, String schoolIdStr) {

//        Integer count = bookStudentMapper.batchAdd(bookOrder);
        Integer count = 0;
        Long schoolId = bookOrder.getSchoolId();
//        if (schoolId == null) {

        if (bookOrder.getBookTextId().equals(1900085161890938885L) && bookOrder.getSubjectsId().equals(1L)) {
            return null;
        }

        Integer beforeLessonNum = 0;
        if (bookOrder.getLessonNum() != null) {
            if (bookOrder.getLessonNum() == 2) {
                beforeLessonNum = 1;
            } else if (bookOrder.getLessonNum() > 2) {
                beforeLessonNum = bookOrder.getLessonNum() - 2;
            }
        } else {
            bookOrder.setLessonNum(0);
        }
        bookOrder.setBeforeLessonNum(beforeLessonNum);

        Integer rade = 0;
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("class_type_id", bookOrder.getClassTypeId());
        wrapper.eq("subjects_id", bookOrder.getSubjectsId());
        BookAccuracySet bookAccuracySet = iBookAccuracySetService.getOne(wrapper);
        if (bookAccuracySet != null) {
            rade = bookAccuracySet.getRade();
        }

        count = bookStudentMapper.batchAdd(bookOrder, schoolIdStr, rade);
//        } else {
//            count = bookStudentMapper.insertion(bookOrder, String.valueOf(schoolId));
//        }
        return count;
    }

    @Override
    public String getBatchName(Integer yearPart, Integer quarterNum, Long subjectsId, Long classTypeId, Integer bookTextType, Long bookTextId, Integer lessonNum, Integer subscribeType) {
        StringBuilder batchNameBd = new StringBuilder();
        //batchNameBd.append(yearPart);
        //String quarterName = iDictService.getDictNameByNumAndType(quarterNum, 19);
        //batchNameBd.append(quarterName);
        //String subjectsName = CommonUtil.getSubjectsNameForId(Integer.parseInt(subjectsId.toString()));
        //batchNameBd.append(subjectsName);
        //String classTypeName = iCourseTypeService.getNameForId(classTypeId);
        //classTypeName.replace("班", "");
        //batchNameBd.append(classTypeName);
        Integer maxBatchNum = getMaxBatchNum(yearPart, quarterNum, subjectsId, classTypeId, bookTextType, bookTextId, lessonNum, subscribeType);
        batchNameBd.append("第").append(maxBatchNum).append("次");
        return batchNameBd.toString();
    }

    @Override
    public Integer getMaxBatchNum(Integer yearPart, Integer quarterNum, Long subjectsId, Long classTypeId, Integer bookTextType, Long bookTextId, Integer lessonNum, Integer subscribeType) {
        QueryWrapper<BookOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year_part", yearPart);
        queryWrapper.eq("quarter_num", quarterNum);
        queryWrapper.eq("subjects_id", subjectsId);
        queryWrapper.eq("class_type_id", classTypeId);
        queryWrapper.eq("book_text_type", bookTextType);
        queryWrapper.eq("book_text_id", bookTextId);
        queryWrapper.select("IFNULL(MAX(batch_num),0) as batch_num");
        queryWrapper.last("limit 1");
        if (subscribeType == 1) {
            queryWrapper.eq("lesson_num", lessonNum);
        }
        BookOrder bookOrder = bookOrderMapper.selectOne(queryWrapper);
        bookOrder.setBatchNum(bookOrder.getBatchNum() + 1);
        return bookOrder.getBatchNum();
    }


    /**
     * 下单
     *
     * @param bookOrderParam
     * @return
     */
    @Override
    public boolean placeOrder(BookOrder bookOrderParam, String schoolIdStr) {

        /*StringBuilder batchNameBd = new StringBuilder();
        batchNameBd.append(bookOrderParam.getYearPart());
        String quarterName = iDictService.getDictNameByNumAndType(bookOrderParam.getQuarterNum(), 19);
        batchNameBd.append(quarterName);
        String subjectsName = CommonUtil.getSubjectsNameForId(Integer.parseInt(bookOrderParam.getSubjectsId().toString()));
        batchNameBd.append(subjectsName);
        String classTypeName = iCourseTypeService.getNameForId(bookOrderParam.getClassTypeId());
        classTypeName.replace("班", "");
        batchNameBd.append(classTypeName);
        Integer maxBatchNum = getMaxBatchNum(bookOrderParam.getYearPart(), bookOrderParam.getQuarterNum(), bookOrderParam.getSubjectsId(), bookOrderParam.getClassTypeId(), bookOrderParam.getBookTextType(), bookOrderParam.getBookTextId());
        batchNameBd.append("第").append(maxBatchNum).append("批");*/

        Integer lessonNum = 0;
        if (bookOrderParam.getLessonNum() != null) {
            lessonNum = bookOrderParam.getLessonNum();
        } else {
            lessonNum = 0;
        }

        String batchName = getBatchName(bookOrderParam.getYearPart(), bookOrderParam.getQuarterNum(), bookOrderParam.getSubjectsId(), bookOrderParam.getClassTypeId(), bookOrderParam.getBookTextType(), bookOrderParam.getBookTextId(), bookOrderParam.getLessonNum(), bookOrderParam.getSubscribeType());
        Integer maxBatchNum = getMaxBatchNum(bookOrderParam.getYearPart(), bookOrderParam.getQuarterNum(), bookOrderParam.getSubjectsId(), bookOrderParam.getClassTypeId(), bookOrderParam.getBookTextType(), bookOrderParam.getBookTextId(), bookOrderParam.getLessonNum(), bookOrderParam.getSubscribeType());
        bookOrderParam.setBatchName(batchName);

        bookOrderParam.setBatchNum(maxBatchNum);
        String maxBatchNumStr = CommonUtil.padZeroes(maxBatchNum, 3);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateNow = sdf.format(new Date());
        String batchNo = dateNow + maxBatchNumStr;
        bookOrderParam.setBatchNo(batchNo);
        bookOrderParam.setStatus(2);//1订购，2下单，3，领取
        bookOrderParam.setIsOrderStatus(1);//有下单
        bookOrderParam.setBookTextId(bookOrderParam.getBookTextId());
        bookOrderParam.setBookTextType(bookOrderParam.getBookTextType());
        if (bookOrderParam.getSubscribeType() != null) {
            if (bookOrderParam.getSubscribeType() == 1) {
                bookOrderParam.setLessonNum(lessonNum);
            }
        }
        this.save(bookOrderParam);

/*        QueryWrapper<BookOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",bookOrderParam.getId());
        queryWrapper.eq("status",1);
        List<BookOrder> bookOrderList = iBookOrderService.list(queryWrapper);
        if (bookOrderList.size() == 0 || bookOrderList.isEmpty()){
            BookOrder bookOrder = this.getById(bookOrderParam.getId());
            bookOrder.setStatus(2);
            this.updateById(bookOrder);
        }*/

//        BookOrder bookOrder = this.getById(bookOrderParam.getId());
//        bookOrder.setStatus(2);
//        this.updateById(bookOrder);

        UpdateWrapper<BookStudent> updateWrapper = new UpdateWrapper<>();
        updateWrapper.inSql(StringUtils.isBlank(bookOrderParam.getSchoolIdStr()), "school_id", schoolIdStr);
        updateWrapper.inSql(StringUtils.isNotBlank(bookOrderParam.getSchoolIdStr()), "school_id", bookOrderParam.getSchoolIdStr());
        updateWrapper.inSql(StringUtils.isNotBlank(bookOrderParam.getYearClassIdStr()), "year_class_id", bookOrderParam.getYearClassIdStr());
        updateWrapper.eq("class_type_id", bookOrderParam.getClassTypeId());
        updateWrapper.eq("status", 1);
        updateWrapper.eq("subjects_id", bookOrderParam.getSubjectsId());
        updateWrapper.eq("year_part", bookOrderParam.getYearPart());
        updateWrapper.eq("quarter_num", bookOrderParam.getQuarterNum());
        updateWrapper.eq("book_text_id", bookOrderParam.getBookTextId());
        updateWrapper.eq("book_text_type", bookOrderParam.getBookTextType());
        updateWrapper.eq("lesson_num", lessonNum);
        updateWrapper.set("order_id", bookOrderParam.getId());
        updateWrapper.set("status", 2);
        bookStudentMapper.update(null, updateWrapper);

//        QueryWrapper<BookStudent> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("order_id", bookOrderParam.getId());
//        queryWrapper.eq("status", 1);
//        List<BookStudent> bookStudentList = iBookStudentService.list(queryWrapper);

//        BookOrder bookOrder = this.getById(bookOrderParam.getId());
//        if (bookStudentList.size() == 0 || bookStudentList.isEmpty()){
//            BookOrder bookOrder = this.getById(bookOrderParam.getId());
//            bookOrder.setStatus(2);
//            this.updateById(bookOrder);
//        }

        return true;
    }

    @Override
    public IPage<BookSchoolRs> getNotOrderSchoolList(
            IPage<?> page,
            Integer yearPart,
            Integer quarterNum,
            String subjectsId,
            String classTypeId,
            Integer lessonType,
            Integer type,
            Integer status,
            Integer orderType,
            Integer orderSubType,
            String schoolIdStr,
            Long bookTextId,
            Integer bookTextType,
            Integer isDetails,
            Long teacherId,
            Long schoolId,
            Integer yearClassId,
            String teacherName,
            String studentName,
            Long gradeId,
            Integer subscribeType,
            Integer lessonNum
    ) {

        if (bookTextId.equals(1900085161890938885L) && subjectsId.equals("1")) {
            return null;
        }

        Integer beforeLessonNum = 0;
        if (lessonNum != null) {
            if (lessonNum == 2) {
                beforeLessonNum = 1;
            } else if (lessonNum > 2) {
                beforeLessonNum = lessonNum - 2;
            }
        }

        Integer rade = 0;
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("class_type_id", classTypeId);
        wrapper.eq("subjects_id", subjectsId);
        BookAccuracySet bookAccuracySet = iBookAccuracySetService.getOne(wrapper);
        if (bookAccuracySet != null) {
            rade = bookAccuracySet.getRade();
        }

        IPage<BookSchoolRs> bookSchoolRsIPage = bookOrderMapper.getNotOrderSchoolList(page, yearPart, quarterNum, subjectsId, classTypeId, lessonType, type, status, orderType, orderSubType, schoolIdStr, bookTextId, bookTextType, isDetails, teacherId, schoolId, yearClassId, teacherName, studentName, gradeId, subscribeType, lessonNum, beforeLessonNum, rade);
        if (isDetails != 2) {
            for (BookSchoolRs bookSchoolRs : bookSchoolRsIPage.getRecords()) {
                bookSchoolRs.setBookCountList(bookOrderMapper.getNotOrderSchoolCountList(yearPart, quarterNum, bookSchoolRs.getSchoolId(), subjectsId, classTypeId, lessonType, isDetails, bookSchoolRs.getTeacherId(), yearClassId, bookTextId, bookTextType, subscribeType, lessonNum, beforeLessonNum, rade));
            }
        }

        return bookSchoolRsIPage;
    }

    @Override
    public List<BookClassTypeCountRs> getNotOrderSchoolCountForClassType(Integer yearPart,
                                                                         Integer quarterNum,
                                                                         String subjectsId,
                                                                         String classTypeId,
                                                                         Integer lessonType,
                                                                         Long bookTextId,
                                                                         Integer bookTextType,
                                                                         Integer lessonNum,
                                                                         Integer subscribeType
    ) {
        Integer beforeLessonNum = 0;
        if (lessonNum != null) {
            if (lessonNum == 2) {
                beforeLessonNum = 1;
            } else if (lessonNum > 2) {
                beforeLessonNum = lessonNum - 2;
            }
        }

        Integer rade = 0;
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("class_type_id", classTypeId);
        wrapper.eq("subjects_id", subjectsId);
        BookAccuracySet bookAccuracySet = iBookAccuracySetService.getOne(wrapper);
        if (bookAccuracySet != null) {
            rade = bookAccuracySet.getRade();
        }

        return bookOrderMapper.getNotOrderSchoolCountForClassType(yearPart, quarterNum, subjectsId, classTypeId, lessonType, bookTextId, bookTextType, lessonNum, subscribeType, beforeLessonNum, rade);
    }

    @Override
    public IPage<BookSchoolRs> getCancelOrderList(IPage<?> page, Integer yearPart, Integer quarterNum, String subjectsId, String classTypeId, Integer lessonType, String schoolIdStr, Long bookTextId, Integer bookTextType, Integer isDetails, Long teacherId, Long schoolId, Integer yearClassId, String teacherName, String studentName, Long gradeId, Integer lessonNum, Integer type) {
        return bookOrderMapper.getCancelOrderList(page, yearPart, quarterNum, subjectsId, classTypeId, lessonType, schoolIdStr, bookTextId, bookTextType, isDetails, teacherId, schoolId, yearClassId, teacherName, studentName, gradeId, lessonNum, type);
    }

    @Override
    public List<BookSchoolCountRs> getCancelOrderCountList(Integer yearPart, Integer quarterNum, String subjectsId, String classTypeId, Integer lessonType, String schoolIdStr, Long bookTextId, Integer bookTextType, Integer isDetails, Long schoolId, Long teacherId, Integer yearClassId, Integer lessonNum) {
        return bookOrderMapper.getCancelOrderCountList(yearPart, quarterNum, subjectsId, classTypeId, lessonType, schoolIdStr, bookTextId, bookTextType, isDetails, schoolId, teacherId, yearClassId, lessonNum);
    }

}
